#!/usr/bin/node

const log = console.log;
const koa = require('koa'),
      Router = require('koa-router'),
      bodyparser = require('koa-bodyparser');

let router = new Router();
let app = new koa();

router.prefix('/api/tasks');

router.get('/',async(ctx,next) => {
  let {page,limit} = ctx.query;
  console.log(ctx.header['user-agent']);
  log(page);
  log(limit);
  log(`得到待办事件列表: page=${page},limit=${limit}`);
  ctx.body='查询成功';
})

router.post('/',async (ctx,next)=>{
  let {task} = ctx.request.body;

  log(`得到待办事件列表,task=${task}`);
  ctx.body='添加成功'; 
})

router.put('/:id',async(ctx,next)=>{
  let {id} = ctx.params;
  let {task} = ctx.request.body;

  log(`修改待办事件列表 id=${id} task=${task}`);
  ctx.body='修改成功';
})

router.delete('/:id',async(ctx,next)=>{
  let {id} = ctx.params;
  log(`删除待办事件列表 id=${id}`);
  ctx.body='删除成功';
})

app.use(bodyparser({enableTypes:['json','form','text']}));
app.use(router.routes());
app.listen(8080);

log('hello world');

